home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 January: Mac OS SDK / Dev.CD Jan 96 SDK / Dev.CD Jan 96 SDK1.toast / Development Kits (Disc 1) / AOCE / Development Tools / Sample Code / Messaging Service Access Module / Internet PMSAM / Internet PMSAM source / authstuff.c next >
Encoding:
C/C++ Source or Header  |  1993-06-28  |  5.2 KB  |  231 lines  |  [TEXT/MPS ]

  1. /*-------------------------------------------------------------------
  2.  
  3. AOCE Post Office Protocol (POP) / Simple Mail Transfer Protocol (SMTP)
  4. Mail Service Access Module
  5.  
  6. written by Steve Falkenburg-- MacDTS
  7. ©1991-1993 Apple Computer, Inc.
  8.  
  9. --------------
  10. change history
  11. --------------
  12.  
  13. SJF        02/19/93    update for beta build    b1
  14. SJF        10/29/92    update to a11            a11
  15. SJF        06/08/92    update to a8            a8
  16. SJF        02/15/92    first working version    a4.5
  17. SJF        10/16/91    initial coding            a3
  18.  
  19. ---------------------------------------------------------------------*/
  20.  
  21. #ifndef __OCE__
  22. #include <OCE.h>
  23. #endif
  24.  
  25. #ifndef __OCEAUTHDIR__
  26. #include <OCEAuthDir.h>
  27. #endif
  28.  
  29. #include <string.h>
  30.  
  31. #include "const.h"
  32. #include "gwerrors.h"
  33. #include "mytypes.h"
  34. #include "globals.h"
  35. #include "utils.h"
  36. #include "gatewaystuff.h"
  37. #include "authstuff.h"
  38.  
  39. OSErr InitAuthStuff(void)
  40. {
  41.     AuthParamBlock authBlock;
  42.     OSErr err;
  43.     Str255 appName;
  44.     short resRefNum;
  45.     Handle hParms;
  46.     
  47.     gAuthInCriticalSection = 0;
  48.     gAuthUnlocked = false;
  49.     gAuthRefresh = false;
  50.     
  51.     err = AuthGetLocalIdentity(&authBlock,false);
  52.     if (err==noErr) {
  53.         gAuthUnlocked = true;
  54.         gAuthRefresh = true;
  55.     }
  56.     gLocalIdentity = authBlock.getLocalIdentityPB.theLocalIdentity;
  57.     
  58.     GetAppParms(appName,&resRefNum,&hParms);
  59.     authBlock.localIdentityQInstallPB.notifyProc = AuthNotifyProc;
  60.     authBlock.localIdentityQInstallPB.notifyFlags = kNotifyLockMask|kNotifyUnlockMask;
  61.     authBlock.localIdentityQInstallPB.appName = appName;
  62.     err = AuthAddToLocalIdentityQueue(&authBlock,false);
  63.     
  64.     return err;
  65. }
  66.  
  67.  
  68. OSErr ExitAuthStuff(void)
  69. {
  70.     AuthParamBlock authBlock;
  71.     OSErr err;
  72.     
  73.     TraceExecution("\pExitAuthStuff");
  74.     
  75.     authBlock.localIdentityQRemovePB.notifyProc = AuthNotifyProc;
  76.     err = AuthRemoveFromLocalIdentityQueue(&authBlock,false);
  77.     return err;
  78. }
  79.  
  80.  
  81. void EnterAuthCriticalSection(void)
  82. {
  83.     gAuthInCriticalSection++;
  84. }
  85.  
  86.  
  87. void ExitAuthCriticalSection(void)
  88. {
  89.     gAuthInCriticalSection--;
  90. }
  91.  
  92.  
  93. OSErr CheckAuthRefresh(void)
  94. {
  95.     OSErr err;
  96.     AuthParamBlock authBlock;
  97.     
  98.     // exit if we don't need to refresh data
  99.     if (!gAuthRefresh)
  100.         return noErr;
  101.     
  102.     if (gAuthUnlocked) {
  103.         AuthGetLocalIdentity(&authBlock,false);    // they might have just configured, so this wouldn't be correct yet
  104.         gLocalIdentity = authBlock.getLocalIdentityPB.theLocalIdentity;
  105.         err = ReadDirectoryInfo();
  106.     }
  107.     else
  108.         err = ClearDirectoryInfo();
  109.         
  110.     gAuthRefresh = false;
  111.     
  112.     return err;
  113. }
  114.  
  115.  
  116. /*--------------------------------------------------------------------------------*/
  117.  
  118.  
  119. pascal Boolean AuthNotifyProc(long clientData, AuthLocalIdentityOp callValue,
  120.             AuthLocalIdentityLockAction actionValue, LocalIdentity identity)
  121. {
  122.     #pragma unused (clientData,identity)
  123.  
  124.     #define    kAllowLock        false
  125.     #define    kPreventLock    true
  126.     
  127.     // if they're requesting a lock, deny only if we're in a critical auth section
  128.     
  129.     if (actionValue==kAuthLockPending)
  130.         return (gAuthInCriticalSection!=0);
  131.  
  132.     // set locked/unlocked flag
  133.     
  134.     if (callValue==kAuthUnlockLocalIdentityOp)
  135.         gAuthUnlocked = true;
  136.     else
  137.         gAuthUnlocked = false;
  138.         
  139.     gAuthRefresh = true;
  140.     gWakeUp = true;            // set a flag to make sure we affect the *next* WaitNextEvent
  141.     WakeUpProcess(&gOurPSN);
  142.  
  143.     return kAllowLock;
  144. }
  145.  
  146.  
  147. // ReadDirectoryInfo
  148. //
  149. // this is called when we need to fill in the name and password for the external system
  150. // for each slot, we read in the name and password for the directory
  151.  
  152. OSErr ReadDirectoryInfo(void)
  153. {
  154.     short slotIndex;
  155.     OSErr err;
  156.     
  157.     err = noErr;
  158.         
  159.     for (slotIndex=0;slotIndex<gNumSlots && err==noErr;slotIndex++)
  160.         err = ReadSingleDirectoryInfo(&gSlotDatabase[slotIndex].directoryName,
  161.                     &gSlotDatabase[slotIndex].discriminator,
  162.                     &gSlotDatabase[slotIndex].dirIdentity);
  163.     
  164.     return err;
  165. }
  166.  
  167.  
  168. // ReadSingleDirectoryInfo
  169. //
  170. // called for each slot from ReadDirectoryInfo, this call does the actual work of reading the
  171. // info into the database
  172.  
  173. OSErr ReadSingleDirectoryInfo(const RString *dirName,const DirDiscriminator *discriminator,DirIdentity *dirIdentity)
  174. {
  175.     RString rsNativeName,rsPassword,ridName,ridType;
  176.     PackedRLI pRLI;
  177.     RecordID identRID;
  178.     OCESetupGetDirectoryInfoPB authBlock;
  179.     OSErr err;
  180.  
  181.     TraceExecution("\pREADDIRECTORYINFO");    
  182.     
  183.     rsNativeName.charSet = smRoman;
  184.     rsNativeName.dataLength = kRStringMaxChars;
  185.     rsPassword.charSet = smRoman;
  186.     rsPassword.dataLength = kRStringMaxChars;
  187.     ridName.charSet = smRoman;
  188.     ridName.dataLength = kRStringMaxChars;
  189.     ridType.charSet = smRoman;
  190.     ridType.dataLength = kRStringMaxChars;
  191.     pRLI.dataLength = 0;
  192.     identRID.rli = &pRLI;
  193.     OCESetCreationIDtoNull(&identRID.local.cid);
  194.     identRID.local.recordName = &ridName;
  195.     identRID.local.recordType = &ridType;
  196.     
  197.     authBlock.directoryName = (DirectoryName *)dirName;
  198.     authBlock.discriminator = *discriminator;
  199.     authBlock.recordID = &identRID;
  200.     authBlock.nativeName = &rsNativeName;
  201.     authBlock.password = &rsPassword;
  202.     err = OCESetupGetDirectoryInfo((AuthParamBlockPtr)&authBlock,false);
  203.     if (err!=noErr)
  204.         return err;
  205.  
  206.     r2cString(&ridName,dirIdentity->userName);
  207.     r2cString(&rsPassword,dirIdentity->password);
  208.     dirIdentity->valid = true;
  209.  
  210.     return err;
  211. }
  212.  
  213.  
  214. OSErr ClearDirectoryInfo(void)
  215. {
  216.     DirIdentity *dirIdentity;
  217.     short slotIndex;
  218.     
  219.     TraceExecution("\pCLEARDIRECTORYINFO");    
  220.     
  221.     for (slotIndex=0;slotIndex<gNumSlots;slotIndex++) {
  222.         dirIdentity = &gSlotDatabase[slotIndex].dirIdentity;
  223.         dirIdentity->userName[0] = 0;
  224.         dirIdentity->password[0] = 0;
  225.         dirIdentity->valid = false;
  226.     }    
  227.     
  228.     return noErr;
  229. }
  230.  
  231.